package com.jiangdg.usbcamera.usbcamera;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import androidx.appcompat.app.AlertDialog;
import com.jiangdg.usbcamera.R;
import java.util.List;

public class ResolutionListDialog {


    /**
     * 显示分辨率
     *
     * @param context
     * @param list
     */
    public static void showResolutionListDialog(Context context, List<String> list) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        View rootView = LayoutInflater.from(context).inflate(R.layout.layout_dialog_list, null);
        builder.setView(rootView);
        AlertDialog  mDialog = builder.create();


        ListView listView = (ListView) rootView.findViewById(R.id.listview_dialog);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, list);
        if (adapter != null) {
            listView.setAdapter(adapter);
        }

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                if (USBCameraManager.getInstance().isCameraOpened()){
                    final String resolution = (String) adapterView.getItemAtPosition(position);
                    String[] tmp = resolution.split("x");
                    if (tmp != null && tmp.length >= 2) {
                        int width = Integer.valueOf(tmp[0]);
                        int height = Integer.valueOf(tmp[1]);
                        USBCameraManager.getInstance().updateResolution(width, height);
                    }
                    mDialog.dismiss();
                }
            }
        });
        mDialog.show();
    }
}
